package com.wangzhixuan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangzhixuan.commons.filestorage.UploadResult;
import com.wangzhixuan.mapper.SysFileMapper;
import com.wangzhixuan.model.SysFile;
import com.wangzhixuan.service.ISysFileService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements ISysFileService {
    /**
     * 保存文件记录
     *
     * @param result result
     * @param remark 备注
     */
    private SysFile saveFileData(UploadResult result, String remark) {
        SysFile sysFile = new SysFile();
        sysFile.setRemark(remark);
        sysFile.setName(result.getFileName());
        sysFile.setSize(result.getSize());
        sysFile.setPath(result.getPath());
        sysFile.setUrl(result.getUrl());
        sysFile.setNewName(result.getNewFileName());
        sysFile.setSuffix(result.getSuffix());
        sysFile.setCreateTime(new Date());
        sysFile.setUpdateTime(new Date());
        baseMapper.insert(sysFile);
        return sysFile;

    }

    @Override
    public List<SysFile> saveFiles(List<UploadResult> files) {
        List<SysFile> sysFiles = new ArrayList<>(files.size());
        for (UploadResult uploadResult : files) {
            SysFile sysFile = saveFileData(uploadResult, null);
            sysFiles.add(sysFile);
        }
        return sysFiles;
    }
    @Override
    public SysFile saveFile(String fileName, String suffix, long fileSize, String savePath, String url, String remark) {
        SysFile sysFile = new SysFile();
        sysFile.setDeleted(false);
        sysFile.setCreateTime(new Date());
        sysFile.setUpdateTime(new Date());
        sysFile.setRemark(remark);
        sysFile.setName(fileName);
        sysFile.setSuffix(suffix);
        sysFile.setSize(fileSize);
        sysFile.setPath(savePath);
        sysFile.setUrl(url);
        baseMapper.insert(sysFile);
        return sysFile;
    }
}
